<?php
/*Cette classe va gérer de manière centralisée les évenements du site*/
class KTDEventHandler{
    public static $_MSG = array();
    public function canHandle($event){
        return in_array($event, __CLASS_::$_MSG);
    }
}
/********************/
class KTDFicheHandler extends KTDEventHandler{
    static $onValidation = "onValidation";
    static $onDevalidation = "onDevalidation";

    static $_MSG = array("onValidation", "onDevalidation");

    public function canHandle($event){
        return in_array($event, KTDFicheHandler::$_MSG);
    }
    public static function onValidation($values){       
            $character = $values["character"];
            //On crée la notification !
            $not = new KTDNotification();
            $not->setType("fiche/validation");
            $not->serializeDatas(array("character_id" => $character->getId()));
            $not->setPlayer($character->getKTDPlayer());
            $not->save();

    }
    public static function onDevalidation($values){
        $character = $values["character"];
        //On crée la notification !
        $not = new KTDNotification();
        $not->setType("fiche/devalidation");
        $not->serializeDatas(array("character_id" => $character->getId()));
        $not->setPlayer($character->getKTDPlayer());
        $not->save();

        
    }
}
class KTDTopicHandler extends KTDEventHandler{
    static $onDelete = "onDelete";
    static $onCreate = "onCreate";

    static $_MSG = array("onCreate", "onDelete");

    public function canHandle($event){
        return in_array($event, KTDTopicHandler::$_MSG);
    }
    public static function onDelete($values){
        $topic = $values["topic"];
        foreach($topic->getPlayers() as $player){
            $player->decMsgNb();
            $player->save();
        }
    }
    public static function onCreate($values){
        $player = $values["player"];

        $player->incMsgNb();
        $player->save();
       
    }

}
class KTDMessageHandler extends KTDEventHandler{

    static $onDelete = "onDelete";
    static $onEdit   = "onEdit";
    static $onPost   = "onPost";

    static $_MSG = array("onPost", "onDelete", "onEdit");

    public function canHandle($event){
        return in_array($event, KTDMessageHandler::$_MSG);
    }
    public static function onDelete($values){
        $player = $values["player"];
        $player->decMsgNb();
        $player->save();
    }
    public static function onPost($values){
        $player = $values["player"];
        $amount = $values["amount"];
        $player->giveMoney($amount);
        $player->incMsgNb();
        $player->save();
    }
}
class KTDActionHandler extends KTDEventHandler{
    static $onPost = "onPost";
    static $onEdit = "onEdit";
    static $onDelete = "onDelete";

    static $_MSG = array("onPost", "onDelete", "onEdit");
    public function canHandle($event){
        return in_array($event, KTDActionHandler::$_MSG);
    }
    public static function onPost($values){
        $player = $values["player"];
        $amount = $values["amount"];
        $adv    = $values["adventure"];
        $player->giveMoney($amount);
        $player->save();
        //notification des acteurs
        foreach($adv->getActors() as $c){
            $not = new KTDNotification();
            $not->setType("adventure/new-action");
            $not->setPlayer($c->getKTDPlayer());
            $not->serializeDatas(array("adventure_id"=>$adv->getId()));
            $not->save();
        }
        //notification des watchers
        foreach($adv->getKTDAdventureWatch() as $w){
            $not = new KTDNotification();
            $not->setType("adventure/new-action");
            $not->setPlayer($w->getPlayer());
            $not->serializeDatas(array("adventure_id"=>$adv->getId()));
            $not->save();
        }
    }
}

class KTDAdventureHandler extends KTDEventHandler{
    static $onCreate = "onCreate";
    static $onDelete = "onDelete";

    static $_MSG = array("onCreate", "onDelete");
    public function canHandle($event){
        return in_array($event, KTDAdventureHandler::$_MSG);
    }
    public static function onCreate($values){
        $player = $values["player"];
        $amount = $values["amount"];
        $player->giveMoney($amount);
        $player->save();
    }
}
/********************/
class KTDEventManager{
    static $_CAT = array("action"   => "Action",
                         "adventure"=> "Adventure",
                         "message"  => "Message",
                         "topic"    => "Topic",
                         "fiche"    => "Fiche");

    static $_EVENT_HANDLER_CLASS = "Handler";
    static $_EVENT_HANDLER_PREFIX = "KTD";

    public static function getSingleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
    private static $instance;

    //qques fonction rapide 
    public static function handleTopic($event, $values){
        return KTDEventManager::handle("topic", $event, $values);
    }
    public static function handleAdventure($event, $values){
        return KTDEventManager::handle("adventure", $event, $values);
    }
    public static function handleAction($event, $values){
       return KTDEventManager::handle("action", $event, $values);
    }
    public static function handleMessage($event, $values){
       return KTDEventManager::handle("message", $event, $values);
    }
    public static function handleFiche($event, $values){
       return KTDEventManager::handle("fiche", $event, $values);
    }
    public static function handle($cat, $event, $values = array()){
        if(array_key_exists($cat, KTDEventManager::$_CAT)){
            $handler_class = KTDEventManager::$_EVENT_HANDLER_PREFIX.
                             KTDEventManager::$_CAT[$cat].
                             KTDEventManager::$_EVENT_HANDLER_CLASS;

            $rc = new ReflectionClass($handler_class);
            $handler_object = $rc->newInstance();

            if($handler_object->canHandle($event)){
               $value = call_user_func_array(array($handler_object, $event), array($values));
                return $value;
            }
        }
    }

}

?>